package com.cicigodd.brick.plug.system.login.controller;

import com.cicigodd.brick.plug.annotation.resource.PostResource;
import com.cicigodd.brick.plug.common.response.Result;
import com.cicigodd.brick.plug.common.response.ResultGenerator;
import com.cicigodd.brick.plug.system.login.request.DyMiniLoginRequest;
import com.cicigodd.brick.plug.system.login.request.LoginRequest;
import com.cicigodd.brick.plug.system.login.request.WxMiniLoginRequest;
import com.cicigodd.brick.plug.system.login.service.LoginService;
import com.cicigodd.brick.plug.system.login.response.LoginResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Api(tags = "登录退出管理")
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    @ApiOperation(value = "系统用户登录", notes = "系统用户登录")
    @PostResource(name = "系统用户登录", path = "/login", requiredPermission = false)
    public Result<LoginResponse> login(@RequestBody @Validated LoginRequest loginRequest) {
        LoginResponse loginResponse = loginService.login(loginRequest);
        return ResultGenerator.successResult(loginResponse);
    }

    @ApiOperation(value = "系统用户退出", notes = "系统用户退出")
    @PostResource(name = "系统用户退出", path = "/logout")
    public Result<LoginResponse> logout() {
        loginService.logout();
        return ResultGenerator.successResult();
    }

    @ApiOperation(value = "微信用户登录", notes = "微信用户登录")
    @PostResource(name = "微信用户登录", path = "/wxlogin", requiredPermission = false)
    public Result<LoginResponse> wxlogin(@RequestBody @Validated WxMiniLoginRequest wxMiniLoginRequest) {
        LoginResponse wxLoginResponse = null;
        try {
            wxLoginResponse = loginService.wxlogin(wxMiniLoginRequest);
        } catch (Exception e) {
            return ResultGenerator.failResult("登录失败，请检查参数");
        }
        return ResultGenerator.successResult(wxLoginResponse);
    }


}
